package com.example.xiaolu.service.impl;
import java.io.IOException;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xiaolu.mapper.UserMapper;
import com.example.xiaolu.model.domain.User;
import com.example.xiaolu.service.UserService;
import com.example.xiaolu.utils.MyDigest;
import com.example.xiaolu.utils.TransferBase64;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author QiA
* @description 针对表【user(用户)】的数据库操作Service实现
* @createDate 2024-06-29 10:53:27
*/
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {
    public static final String USER_LOGIN_STATE="userLoginState";
    @Override
    public long userRegister(@NotNull String userAccount, String userPassword, String checkPassword){
        if (userAccount.length()<4) {
            return -1;
        }
        if (userPassword.length()<8) {
            return -2;
        }

        // 定义不允许的特殊字符
        String specialCharacters = "[!@#$%^&*()_+=|<>?{}\\[\\]~-]";
        // 创建正则表达式，确保字符串不包含这些特殊字符
        Pattern pattern = Pattern.compile(specialCharacters);
        Matcher matcher = pattern.matcher(userAccount);
        if (matcher.find()) {
            return -3;
        }

        //密码和校验密码不能相同
        if (!userPassword.equals(checkPassword)) {
            return -4;
        }

        //账户不能重复
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        QueryWrapper<User> useraccount = queryWrapper.eq("userAccount", userAccount);
        if (this.count(useraccount) >0) {
            return -5;
        }

        //password加密
        String digestedPassword= MyDigest.digest(userPassword);

        //插入数据库
        User user=User.getInstance();
        user.setUserAccount(userAccount);
        user.setUserPassword(digestedPassword);
        this.save(user);
        System.out.println(user.getId());
        return user.getId();
    }

    @Override
    public User userLogin(String userAccount, String userPassword, HttpServletRequest httpServletRequest) {
        if (userAccount.length()<4) {
            return null;
        }
        if (userPassword.length()<8) {
            return null;
        }

        // 定义不允许的特殊字符
        String specialCharacters = "[!@#$%^&*()_+=|<>?{}\\[\\]~-]";
        // 创建正则表达式，确保字符串不包含这些特殊字符
        Pattern pattern = Pattern.compile(specialCharacters);
        Matcher matcher = pattern.matcher(userAccount);
        if (matcher.find()) {
            return null;
        }

        //password加密
        String digestedPassword= MyDigest.digest(userPassword);
        //账户不能重复
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userAccount", userAccount);
        queryWrapper.eq("userPassword", digestedPassword);
        User user = this.getOne(queryWrapper);
        if (user==null) {
            log.info("user login failed,password not match account");
            return null;
        }

        //脱敏
        User safetyUser=User.getInstance();
        safetyUser.setId(user.getId());
        safetyUser.setUsername(user.getUsername());
        safetyUser.setUserAccount(user.getUserAccount());
        safetyUser.setGender(user.getGender());
        safetyUser.setPhone(user.getPhone());
        safetyUser.setEmail(user.getEmail());
        safetyUser.setUserStatus(user.getUserStatus());
        safetyUser.setCreateTime(user.getCreateTime());
        safetyUser.setUserRole(user.getUserRole());
        if (user.getAvatarUrl().equals(user.getId()+"")) {
            safetyUser = TransferBase64.getAvtar(user);
        }else {
            safetyUser.setAvatarUrl(user.getAvatarUrl());
        }



        //记录用户登录状态
        httpServletRequest.getSession().setAttribute(USER_LOGIN_STATE,safetyUser);
        return safetyUser;
    }

    @Override
    public List<User> getAllUsers() {
        List<User> tmp=this.list();
        for (int i = 0; i < tmp.size(); i++) {
            if (!(tmp.get(i).getAvatarUrl() == null)) {
                if (tmp.get(i).getAvatarUrl().equals(tmp.get(i).getId()+"")) {
                    tmp.set(i,TransferBase64.getAvtar(tmp.get(i)));
                }
            }

        }
        return tmp;
    }

    @Override
    public boolean updateUser(User user) {
        //password加密
        String digestedPassword= MyDigest.digest(user.getUserPassword());
        user.setUserPassword(digestedPassword);
        //设置头像到文件
        return this.updateById(TransferBase64.changeUserAvatar(user));
    }
}




